package sys_serv

import (
	"strconv"
	"strings"
	"ubt-gin-admin/app/api/model/sys_mod"
	"ubt-gin-admin/app/pkg/db"
	"ubt-gin-admin/app/pkg/enums"
)

type sysTag struct{}

var SysTagServ = sysTag{}

// Add 新增标签
func (s *sysTag) Add(req *sys_mod.SysTagSaveReq) (string, string) {
	sqlStr := "INSERT INTO sys_tag (tag_id, tag_name, kind, states, add_time, upd_time) VALUES ($1, $2, $3, 1, EXTRACT(epoch FROM NOW()), EXTRACT(epoch FROM NOW()))"
	return db.CudData(sqlStr, req.TagId, req.TagName, req.Kind)
}

// Exists 判断标签是否存在
func (s *sysTag) Exists(req *sys_mod.SysTagExtVo) bool {
	sqlStr := "SELECT tag_id FROM sys_tag WHERE states=1 AND kind=$1 AND tag_name=$2"
	param := make([]any, 0)
	param = append(param, req.Kind)
	param = append(param, req.TagName)
	if req.TagId != "" {
		sqlStr += " AND tag_id!=$3"
		param = append(param, req.TagId)
	}
	if _, _, data := db.GetOne(sqlStr, param...); len(data) == 0 {
		return false
	}
	return true
}

// Update 修改标签
func (s *sysTag) Update(req *sys_mod.SysTagSaveReq) (string, string) {
	sqlStr := "UPDATE sys_tag SET tag_name=$1, upd_time=EXTRACT(epoch FROM NOW()) WHERE tag_id=$2"
	return db.CudData(sqlStr, req.TagName, req.TagId)
}

// Delete 删除标签
func (s *sysTag) Delete(tagId string) (string, string) {
	sqlStr := "UPDATE sys_tag SET states=2, upd_time=EXTRACT(epoch FROM NOW()) WHERE tag_id=$1"
	return db.CudData(sqlStr, tagId)
}

// Deletes 处理标签
func (s *sysTag) Deletes(tagIds string) (string, string) {
	var (
		param []any
		q     string
		n     = 0
	)
	idList := strings.Split(tagIds, ",")
	for _, curId := range idList {
		n++
		q += "$" + strconv.Itoa(n) + ","
		param = append(param, curId)
	}
	q = q[:len(q)-1]
	sqlStr := "UPDATE sys_tag SET states=2, upd_time=EXTRACT(epoch FROM NOW()) WHERE tag_id IN (" + q + ")"
	return db.CudData(sqlStr, param...)
}

// Count 获取标签数量
func (s *sysTag) Count(req *sys_mod.SysTagPageReq) (string, string, any) {
	sqlStr := "SELECT COUNT(1) AS nums FROM sys_tag WHERE states=1 AND kind=$1"
	param := make([]any, 0)
	param = append(param, req.Kind)
	var n = 1
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			n++
			sqlStr += "tag_name LIKE '%'||$" + strconv.Itoa(n) + "||'%' OR "
			param = append(param, name)
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	if code, msg, data := db.GetOne(sqlStr, param...); len(data) > 0 && data["nums"].(int64) > 0 {
		return code, msg, data["nums"]
	}
	return enums.NoData.Code, enums.NoData.Msg, 0
}

// Page 获取标签分页列表
func (s *sysTag) Page(req *sys_mod.SysTagPageReq) (string, string, any) {
	sqlStr := "SELECT tag_id, tag_name, add_time FROM sys_tag WHERE states=1 AND kind=$1"
	param := make([]any, 0)
	param = append(param, req.Kind)
	var n = 1
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			n++
			sqlStr += "tag_name LIKE '%'||$" + strconv.Itoa(n) + "||'%' OR "
			param = append(param, name)
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	sqlStr += " ORDER BY upd_time DESC LIMIT $" + strconv.Itoa(n+1) + " OFFSET $" + strconv.Itoa(n+2)
	param = append(param, req.Limit)
	param = append(param, req.Offset)
	return db.GetAll(sqlStr, param...)
}

// Select 查询标签是否已存在，存在则返回
func (s *sysTag) Select(req *sys_mod.SysTagSelReq) (string, string, any) {
	sqlStr := "SELECT tag_id, tag_name FROM sys_tag WHERE states=1 AND kind=$1 AND tag_name=$2 LIMIT 1"
	return db.GetOne(sqlStr, req.Kind, req.TagName)
}
